package com.ruoyi.web.controller.store;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.store.vo.HmStoreProductVo;
import com.ruoyi.store.bo.HmStoreProductQueryBo;
import com.ruoyi.store.bo.HmStoreProductAddBo;
import com.ruoyi.store.bo.HmStoreProductEditBo;
import com.ruoyi.store.service.IHmStoreProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 商品管理Controller
 *
 * @author ruoyi
 * @date 2021-07-02
 */
@Api(value = "商品管理控制器", tags = {"商品管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/store/product")
public class HmStoreProductController extends BaseController {

    private final IHmStoreProductService iHmStoreProductService;

    /**
     * 查询商品管理列表
     */
    @ApiOperation("查询商品管理列表")
    @PreAuthorize("@ss.hasPermi('store:product:list')")
    @GetMapping("/list")
    public TableDataInfo<HmStoreProductVo> list(@Validated HmStoreProductQueryBo bo) {
        return iHmStoreProductService.queryPageList(bo);
    }

    /**
     * 导出商品管理列表
     */
    @ApiOperation("导出商品管理列表")
    @PreAuthorize("@ss.hasPermi('store:product:export')")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<HmStoreProductVo> export(@Validated HmStoreProductQueryBo bo) {
        List<HmStoreProductVo> list = iHmStoreProductService.queryList(bo);
        ExcelUtil<HmStoreProductVo> util = new ExcelUtil<HmStoreProductVo>(HmStoreProductVo.class);
        return util.exportExcel(list, "商品管理");
    }

    /**
     * 获取商品管理详细信息
     */
    @ApiOperation("获取商品管理详细信息")
    @PreAuthorize("@ss.hasPermi('store:product:query')")
    @GetMapping("/{id}")
    public AjaxResult<HmStoreProductVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iHmStoreProductService.queryById(id));
    }

    /**
     * 新增商品管理
     */
    @ApiOperation("新增商品管理")
    @PreAuthorize("@ss.hasPermi('store:product:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody HmStoreProductAddBo bo) {
        return toAjax(iHmStoreProductService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改商品管理
     */
    @ApiOperation("修改商品管理")
    @PreAuthorize("@ss.hasPermi('store:product:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody HmStoreProductEditBo bo) {
        return toAjax(iHmStoreProductService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除商品管理
     */
    @ApiOperation("删除商品管理")
    @PreAuthorize("@ss.hasPermi('store:product:remove')")
    @Log(title = "商品管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iHmStoreProductService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
